//
//  SatusSelectViewController.m
//  Marketing
//
//  Created by HanenDev on 16/3/7.
//  Copyright © 2016年 Hanen 3G 01. All rights reserved.
//

#import "SatusSelectViewController.h"
#import "SelectedCell.h"


#define STARTX [UIView getWidth:10]

@interface SatusSelectViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSInteger     _selecteRow;
    UITableView   *_tableView;
    NSIndexPath   *_lastIndexPath;
}

@end

@implementation SatusSelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = graySectionColor;
    self.navigationItem.titleView = [ViewTool getNavigitionTitleLabel:self.itemTitle];
    self.navigationItem.leftBarButtonItem=[ViewTool getBarButtonItemWithTarget:self WithAction:@selector(goToBack)];
    
    [self initViews];
}
- (void)initViews{
    
    _selecteRow=-1;
    
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height -64) style:UITableViewStylePlain];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.backgroundColor = graySectionColor;
    _tableView.tableFooterView = [[UIView alloc]init];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_tableView];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCreenW, 40)];
    view.backgroundColor = graySectionColor;
    
    UILabel *selectLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 100, 20)];
    selectLabel.textColor = blackFontColor;
    selectLabel.text = @"请选择";
    [ViewTool setLableFont12:selectLabel];
    [view addSubview:selectLabel];
    
    UIView *lineView =[[UIView alloc]initWithFrame:CGRectMake(0, 40 - 1, KSCreenW, 1)];
    lineView.backgroundColor = grayLineColor;
    [view addSubview:lineView];
    
    _tableView.tableHeaderView = view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _ar.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [SelectedCell cellHeight];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SelectedCell *cell = [SelectedCell cellWithTableView:tableView WithIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLabel .text = _ar[indexPath.row];
    
    if (indexPath.row == _ar.count - 1) {
        cell.li.frame = CGRectMake(0, [SelectedCell cellHeight] - 1, KSCreenW, 1);
    }else{
        cell.li.frame = CGRectMake(cell.titleLabel.x, [SelectedCell cellHeight] - 1, KSCreenW, 1);
    }
    
    if (self.chooseIndexPath == indexPath) {
        cell.tagImageView.image = [UIImage imageNamed:@"HY_list_btn_checkbox_selected"];
        _lastIndexPath = self.chooseIndexPath;
    }
//    if ([_valuetTitle isEqualToString:cell.textLabel.text] ) {
//        cell.contentView.backgroundColor = [UIColor lightGrayColor];
//    }
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_lastIndexPath != indexPath) {
        SelectedCell *lastCell=[tableView cellForRowAtIndexPath:indexPath];
            lastCell.tagImageView.image = [UIImage imageNamed:@"HY_list_btn_checkbox_selected"];
        SelectedCell *cell=[tableView cellForRowAtIndexPath:_lastIndexPath];
            cell.tagImageView.image = [UIImage imageNamed:@"HY_list_btn_checkbox_normal"];
        _lastIndexPath = indexPath;
    }
    
    if ([self.delegate respondsToSelector:@selector(selectStatusWith:withNum:withInt:withIndexPath:withType:)]) {
        [self.delegate selectStatusWith:_ar[indexPath.row]withNum:self.num withInt:(int)indexPath.row + 1 withIndexPath:_lastIndexPath withType:self.isLevelOrFromOrStatusType];
    }
    [self performSelector:@selector(clickToVC) withObject:nil afterDelay:0.5];

}
- (void)clickToVC{
     [self.navigationController popViewControllerAnimated:YES];

}

- (void)goToBack{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
